package com.web.ShoppingDemo.action;

import java.util.Map;

import org.apache.struts2.interceptor.ParameterAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.web.ShoppingDemo.model.BackData;
import com.web.ShoppingDemo.model.Forder;
import com.web.ShoppingDemo.model.SendData;
import com.web.ShoppingDemo.model.User;

@Controller
@Scope("prototype")
public class PayAction extends BaseAction<Object> implements ParameterAware{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Map<String,String[]> parameter;
	@Override
	public void setParameters(Map<String, String[]> arg0) {
		this.parameter = arg0;
	}
	//通过传入的参数的值进行判断放入值栈的值
	//同一个Action处理多个Model
	@Override
	public Object getModel() {
		if(parameter.get("pd_FrpId")!=null)
			return new SendData();
		else
			return new BackData();
	}
	/**
	 * 处理支付的Action
	 * @return
	 */
	public String goBank(){
		//补全参数p2 p3 pa pd
		SendData sendData = (SendData)model;
		Forder forder = (Forder)session.get("oldForder");
		User user = (User)session.get("user");
		sendData.setP2_Order(forder.getId().toString());
		sendData.setP3_Amt(forder.getTotal().toString());
		sendData.setPa_MP(user.getEmail()+","+user.getPhone());
		//存储到request域中
		payService.saveDataToRequest(request, sendData);
		System.out.println(request.get("pd_FrpId"));
		//5.跳转到支付页面
		return "pay";
	}
	/**
	 * 处理传回的参数
	 * *********************************
	 * 存在问题
	 * *********************************
	 * @return
	 */
	public void backBank(){
		BackData backData = (BackData)model;
		boolean isOk = payService.checkBackData(backData);
		if(isOk == true){
			//1:更新订单的状态
			forderService.updateStatusById(Integer.parseInt(backData.getR6_Order()), 2);
			//2:根据邮箱地址发送邮件
			String address = backData.getR8_MP().split(",")[0];
			emailUtil.sendEmail(address, backData.getR6_Order());
			//3:发送手机短信
			String number = backData.getR8_MP().split(",")[1];
			messageUtil.sendMessage(number, backData.getR6_Order());
		}else{
			System.out.println("false========================");
		}
	}

}
